package com.ycl.plus.lm.hardware.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import com.ycl.plus.lm.common.mybatis.core.domain.BaseChangeStatusEntity;
import com.ycl.plus.lm.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ycl.plus.lm.common.idempotent.annotation.RepeatSubmit;
import com.ycl.plus.lm.common.log.annotation.Log;
import com.ycl.plus.lm.common.web.core.BaseController;
import com.ycl.plus.lm.common.mybatis.core.page.PageQuery;
import com.ycl.plus.lm.common.core.domain.R;
import com.ycl.plus.lm.common.core.validate.AddGroup;
import com.ycl.plus.lm.common.core.validate.EditGroup;
import com.ycl.plus.lm.common.log.enums.BusinessType;
import com.ycl.plus.lm.common.excel.utils.ExcelUtil;
import com.ycl.plus.lm.hardware.domain.vo.HardwareSensorVo;
import com.ycl.plus.lm.hardware.domain.bo.HardwareSensorBo;
import com.ycl.plus.lm.hardware.service.IHardwareSensorService;

/**
 * 传感器
 * 前端访问路由地址为:/hardware/sensor
 *
 * @author devil-ycl
 * @date 2024-11-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sensor")
public class HardwareSensorController extends BaseController {

    private final IHardwareSensorService hardwareSensorService;

    /**
     * 查询传感器列表
     */
    @SaCheckPermission("hardware:sensor:list")
    @GetMapping("/list")
    public R<List<HardwareSensorVo>> list(HardwareSensorBo bo) {
        List<HardwareSensorVo> list = hardwareSensorService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出传感器列表
     */
    @SaCheckPermission("hardware:sensor:export")
    @Log(title = "传感器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HardwareSensorBo bo, HttpServletResponse response) {
        List<HardwareSensorVo> list = hardwareSensorService.queryList(bo);
        ExcelUtil.exportExcel(list, "传感器", HardwareSensorVo.class, response);
    }

    /**
     * 获取传感器详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("hardware:sensor:query")
    @GetMapping("/{id}")
    public R<HardwareSensorVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(hardwareSensorService.queryById(id));
    }

    /**
     * 新增传感器
     */
    @SaCheckPermission("hardware:sensor:add")
    @Log(title = "传感器", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HardwareSensorBo bo) {
        return toAjax(hardwareSensorService.insertByBo(bo));
    }

    /**
     * 修改传感器
     */
    @SaCheckPermission("hardware:sensor:edit")
    @Log(title = "传感器", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HardwareSensorBo bo) {
        return toAjax(hardwareSensorService.updateByBo(bo));
    }

    /**
     * 删除传感器
     *
     * @param ids 主键串
     */
    @SaCheckPermission("hardware:sensor:remove")
    @Log(title = "传感器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hardwareSensorService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("hardware:sensor:edit")
    @Log(title = "传感器", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BaseChangeStatusEntity<String> bo) {
        return toAjax(hardwareSensorService.updateSensorStatus(bo.getId(), bo.getStatus()));
    }

    /**
     * 获取传感器下拉树列表
     */
    @SaCheckPermission("system:sensor:query")
    @GetMapping("/treeselect")
    public R<List<Tree<Long>>> treeselect(HardwareSensorBo sensor) {
        List<HardwareSensorVo> list = hardwareSensorService.selectSensorList(sensor);
        return R.ok(hardwareSensorService.buildSensorTreeSelect(list));
    }
}
